using System;
using System.Text;
using System.Threading;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.NotificationServices
{
	internal class SubscriptionNotification
	{
		protected internal int subsid;

		protected internal string subscription;

		protected internal object lock_Renamed;

		protected internal bool waiter;

		protected internal SubscriptionException ex;

		protected internal bool success;

		private bool replyrecvd;

		private static sbyte[] subsmessageline;

		private static sbyte[] subsidheader;

		private static sbyte[] subsheader;

		private static sbyte[] contentlengthline;

		protected internal SubscriptionNotification(int sid, string subs, bool w)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.ctor);
			}
			subsid = sid;
			subscription = subs;
			waiter = w;
			ex = null;
			success = false;
			replyrecvd = false;
			if (waiter)
			{
				lock_Renamed = new object();
			}
			else
			{
				lock_Renamed = null;
			}
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.ctor);
			}
		}

		protected internal virtual void waitForReply(long timeout)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.waitForReply);
			}
			try
			{
				lock (lock_Renamed)
				{
					if (!replyrecvd)
					{
						if (timeout <= 0)
						{
							Monitor.Wait(lock_Renamed);
						}
						else
						{
							Monitor.Wait(lock_Renamed, TimeSpan.FromMilliseconds(timeout));
						}
					}
				}
			}
			catch (Exception)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.waitForReply, this.ex);
			}
			try
			{
				if (timeout >= 0 && !replyrecvd)
				{
					throw new SubscriptionException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.ONS_SUBSCR_TIMED_OUT, timeout.ToString()));
				}
				if (!success)
				{
					if (this.ex != null)
					{
						throw this.ex;
					}
					throw new SubscriptionException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.ONS_SUBSCR_FAILED));
				}
			}
			catch (Exception ex2)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.waitForReply, ex2);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.waitForReply);
				}
			}
		}

		protected internal virtual void wakeup(bool s, SubscriptionException sexcept)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.wakeup);
			}
			try
			{
				success = s;
				this.ex = sexcept;
				if (waiter)
				{
					lock (lock_Renamed)
					{
						replyrecvd = true;
						Monitor.PulseAll(lock_Renamed);
					}
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.wakeup, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.wakeup);
				}
			}
		}

		protected internal virtual void send(OutputBuffer obuf)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.send);
			}
			try
			{
				obuf.putBytes(subsmessageline, subsmessageline.Length);
				obuf.putBytes(contentlengthline, contentlengthline.Length);
				obuf.putBytes(subsidheader, subsidheader.Length);
				string text = Convert.ToString(subsid);
				obuf.putBytes(SupportClass.ToSByteArray(SupportClass.ToByteArray(text)), text.Length);
				obuf.putBytes(Notification.crlf, 2);
				obuf.putBytes(subsheader, subsheader.Length);
				if (subscription != null && subscription.Length > 0)
				{
					obuf.putBytes(SupportClass.ToSByteArray(SupportClass.ToByteArray(subscription)), subscription.Length);
				}
				obuf.putBytes(Notification.crlf, 2);
				obuf.putBytes(Notification.crlf, 2);
				obuf.flush();
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.send, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.send);
				}
			}
		}

		static SubscriptionNotification()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.cctor);
			}
			try
			{
				subsmessageline = new sbyte[26];
				Array.Copy(SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("POST /subscribe HTTP/1.1").ToString())), 0, subsmessageline, 0, 24);
				Array.Copy(Notification.crlf, 0, subsmessageline, 24, 2);
				subsidheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("SubscriberID: ").ToString()));
				subsheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("Subscription: ").ToString()));
				contentlengthline = new sbyte[19];
				Array.Copy(SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("Content-Length: 0").ToString())), 0, contentlengthline, 0, 17);
				Array.Copy(Notification.crlf, 0, contentlengthline, 17, 2);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.cctor, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.SubscriptionNotification, OracleTraceFuncName.cctor);
				}
			}
		}
	}
}
